package com.example.experiment5;

import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class SignalStrengthActivity extends AppCompatActivity {
    String wifi_name;
    int    wifi_rssi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_r_s_s_i);

        //定义按键实例
        Button btnRSSI = (Button)findViewById(R.id.wifi_rssi);

        //定义按钮点击事件
        btnRSSI.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取wifi服务
                WifiManager wifiManager= (WifiManager)
                        getApplicationContext().getSystemService(WIFI_SERVICE);
                assert wifiManager != null;
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                wifi_name = wifiInfo.getSSID();

                wifi_rssi = wifiInfo.getRssi();
                Log.d("wifiInfo", wifiInfo.toString());
                Log.d("SSID", wifiInfo.getSSID());

                //通过Toast输出
                Toast.makeText(SignalStrengthActivity.this,
                        "RSSI:" + wifi_rssi + "---WiFi Id:" + wifi_name,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}